<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    
  </style>
</head>
<body>
  <h2>这是登录页面</h2>
  用户名：<input type="text" class="username"><br>
  密码：<input type="text" class="password"><br>
  <input type="button" value="登录" class="btn">
</body>
<script src="../js/get.js"></script>
<script>
  // 1. 选项器
  const loginUrl = "http://10.11.55.86:3000/login";

  // 2. 点击登录按钮
  $(".btn").onclick = function(){
    // 3. 发起登录请求
    ajaxGet({
      url: loginUrl,
      data: {
        username: $(".username").value,
        password: $(".password").value
      },
      success(xhr){
        // 解析数据
        const res = JSON.parse(xhr.responseText);
        console.log(res);
        // 判断是否登录成功
        if(res.code === 0){
          // 4. 将登录成功后的用户信息存到本地存储中
          localStorage.setItem("userData", JSON.stringify(res.data));
          alert(res.msg);
          location.assign("./index.html")
        }else if(res.code === 2){
          alert(res.msg);
          $(".username").value = "";
          $(".username").focus();
        }else if(res.code === 1){
          alert(res.msg);
          $(".password").value = "";
          $(".password").focus();
        }
      }
    })
  }

  function $(s){
    return document.querySelector(s)
  }
</script>
</html>